package com.group9.service.impl;

import com.group9.mapper.ActivityFruitMapper;
import com.group9.mapper.FruitCartMapper;
import com.group9.mapper.FruitInfoMapper;
import com.group9.pojo.*;
import com.group9.service.FruitCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by Lcouncillor on 2020/5/4.
 */
@Service
public class FruitCartServiceImpl implements FruitCartService{
    @Autowired
    FruitCartMapper fruitCartMapper;

    @Autowired
    ActivityFruitMapper activityFruitMapper;

    @Autowired
    FruitInfoMapper fruitInfoMapper;

    @Override
    @Transactional(readOnly = true)
    public List<FruitCart> selectCartByCustomerId(Integer customerId) {
        FruitCartExample fruitCartExample = new FruitCartExample();
        FruitCartExample.Criteria criteria = fruitCartExample.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        return fruitCartMapper.selectByExample(fruitCartExample);
    }

    @Override
    public FruitCart selectCartByCustomerIdAndFruitId(Integer customerId, Integer fruitId) {
        FruitCartExample fruitCartExample = new FruitCartExample();
        FruitCartExample.Criteria criteria = fruitCartExample.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        criteria.andFruitIdEqualTo(fruitId);
        List<FruitCart> fruitCarts = fruitCartMapper.selectByExample(fruitCartExample);
        if (null != fruitCarts && !fruitCarts.isEmpty()){
            return fruitCarts.get(0);
        }
        return null;
    }

    @Override
    @Transactional
    public Integer deleteByCartId(Integer cartId) {
        return fruitCartMapper.deleteByPrimaryKey(cartId);
    }

    @Override
    @Transactional
    public Integer updateCart(FruitCart fruitCart) {
        return fruitCartMapper.updateByPrimaryKeySelective(fruitCart);
    }

    @Override
    public Integer insertCartByFruitIdAndAmount(Integer fruitId, Integer customerId, Integer fruitAmount) {
        FruitCart fruitCart = selectCartByCustomerIdAndFruitId(customerId,fruitId);

        if (fruitCart == null){
            FruitCart fruitCart1 = new FruitCart();
            fruitCart1.setFruitId(fruitId);
            fruitCart1.setCustomerId(customerId);
            fruitCart1.setFruitAmount(fruitAmount);
            fruitCart1.setAddTime(new Date());

            ActivityFruitExample activityFruitExample = new ActivityFruitExample();
            ActivityFruitExample.Criteria criteria = activityFruitExample.createCriteria();
            criteria.andFruitIdEqualTo(fruitId);
            List<ActivityFruit> activityFruits = activityFruitMapper.selectByExample(activityFruitExample);
            if (activityFruits == null){
                fruitCart1.setCartStatus((byte) 1);
                fruitCart1.setRealPrice(fruitInfoMapper.selectByPrimaryKey(fruitId).getFruitPrice());
            }else{
                fruitCart1.setCartStatus((byte) 2);
                fruitCart1.setRealPrice(activityFruits.get(0).getFruitActivityCost());
            }

            return fruitCartMapper.insertSelective(fruitCart1);
        }else{
            fruitCart.setFruitAmount(fruitCart.getFruitAmount()+fruitAmount);
            return updateCart(fruitCart);
        }
    }


}
